package com.person.service.impl;

import cn.hutool.core.util.IdUtil;
import com.person.common.BaseContext;
import com.person.mapper.AddressBookMapper;
import com.person.mapper.UserMapper;
import com.person.pojo.AddressBook;
import com.person.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class AddressBookBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void save(AddressBook addressBook) {

        addressBook.setId(IdUtil.getSnowflakeNextId());
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setCreateUser(BaseContext.getCurrentId());
        addressBook.setUpdateUser(BaseContext.getCurrentId());
        addressBookMapper.insertAddressbook(addressBook);

    }

    @Override
    public List<AddressBook> inquire(Long id) {
        List<AddressBook> list = addressBookMapper.select(id);
        return list;
    }

    @Override
    public void setDefault(Long id) {
        addressBookMapper.closeDefault(BaseContext.getCurrentId());
        addressBookMapper.setDefault(id);
    }

    @Override
    public AddressBook showAddress(Long id) {
        AddressBook addressBook = addressBookMapper.selectAddress(id);
        return addressBook;
    }

    @Override
    public void updateAddress(AddressBook addressBook) {
        addressBook.setUpdateUser(BaseContext.getCurrentId());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBookMapper.update(addressBook);
    }

    @Override
    public void remove(Long ids) {
        addressBookMapper.delete(ids);
    }

    @Override
    public AddressBook showDefaultAddress() {
        return addressBookMapper.showDefaultAddress(BaseContext.getCurrentId());
    }
}
